package ReentrantLock_3;
import java.util.concurrent.locks.ReentrantLock;

class RecursiveTask {
    private final ReentrantLock lock = new ReentrantLock();

    public void performTask(int level) {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + " 进入第 " + level + " 层");
            if (level > 0) {
                performTask(level - 1); // 递归调用
            }
            System.out.println(Thread.currentThread().getName() + " 离开第 " + level + " 层");
        } finally {
            lock.unlock();
        }
    }
}

public class 可重入特性_递归调用 {
    public static void main(String[] args) {
        final RecursiveTask task = new RecursiveTask();

        // 创建20个执行递归任务的线程
        for (int i = 0; i < 20; i++) {
            new Thread(new Runnable() {
                public void run() {
                    task.performTask(3); // 递归3层
                }
            }, "递归线程-" + i).start();
        }
    }
}